package com.lzm.ydpt.mvp.api;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;

import com.byt.framlib.basemvp.basebean.BaseResponseBean;
import com.byt.framlib.basemvp.exception.ApiException;
import com.byt.framlib.basemvp.exception.CodeException;
import com.byt.framlib.basemvp.exception.HttpTimeException;
import com.byt.framlib.basemvp.listener.OnLoadCompleteListener;
import com.byt.framlib.basemvp.listener.OnLoadingErrorListener;
import com.byt.framlib.basemvp.listener.OnLoadingProgressListener;
import com.byt.framlib.commonutils.NetWorkUtils;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 类名称：  ProgressSubscriber
 * 创建时间：2017/10/25
 * 类描述：  订阅请求事件响应类
 */
public class ProgressSubscriber<T> implements Observer<BaseResponseBean<T>> {

    private String mMessage;
    private Context mContext;
    private OnLoadingErrorListener<T> mLoadingErrorListener;
    private OnLoadingProgressListener<T> mLoadingProgressListener;
    private OnLoadCompleteListener mOnLoadCompleteListener;
    private String iName;

    public ProgressSubscriber(Context context, String mMessage) {
        super();
        this.mContext = context;
        this.mMessage = mMessage;
    }

    public ProgressSubscriber(Context context, OnLoadingProgressListener<T>
            onLoadingProgressListener) {
        super();
        this.mContext = context;
        this.mLoadingProgressListener = onLoadingProgressListener;

    }

    public ProgressSubscriber(Context context, String message, OnLoadingProgressListener<T> onLoadingProgressListener) {
        this.mMessage = message;
        this.mContext = context;
        this.mLoadingProgressListener = onLoadingProgressListener;
    }

    public ProgressSubscriber(Context context, String message, OnLoadingProgressListener<T> onLoadingProgressListener, OnLoadCompleteListener mOnLoadCompleteListener) {
        this.mMessage = message;
        this.mContext = context;
        this.mLoadingProgressListener = onLoadingProgressListener;
        this.mOnLoadCompleteListener = mOnLoadCompleteListener;
    }

    public ProgressSubscriber(Context context, OnLoadingProgressListener<T> onLoadingProgressListener,
                              OnLoadingErrorListener onLoadingErrorListener) {
        super();
        this.mContext = context;
        this.mLoadingProgressListener = onLoadingProgressListener;
        this.mLoadingErrorListener = onLoadingErrorListener;
    }

    public ProgressSubscriber(Context context, OnLoadingProgressListener<T> onLoadingProgressListener,
                              OnLoadingErrorListener onLoadingErrorListener, String iName) {
        super();
        this.mContext = context;
        this.mLoadingProgressListener = onLoadingProgressListener;
        this.mLoadingErrorListener = onLoadingErrorListener;
        this.iName = iName;
    }

    public ProgressSubscriber(Context context, OnLoadingProgressListener<T> onLoadingProgressListener, OnLoadingErrorListener onLoadingErrorListener, OnLoadCompleteListener mOnLoadCompleteListener) {
        super();
        this.mContext = context;
        this.mLoadingProgressListener = onLoadingProgressListener;
        this.mLoadingErrorListener = onLoadingErrorListener;
        this.mOnLoadCompleteListener = mOnLoadCompleteListener;
    }

    @SuppressWarnings("unused")
    public void setOnLoadingProgressListener(OnLoadingProgressListener<T> onLoadingProgressListener) {
        mLoadingProgressListener = onLoadingProgressListener;
    }

    @SuppressWarnings("unused")
    public void setOnLoadingErrorListener(OnLoadingErrorListener loadingErrorListener) {
        this.mLoadingErrorListener = loadingErrorListener;
    }

    @SuppressWarnings("unused")
    public void setOnLoadCompleteListener(OnLoadCompleteListener onLoadCompleteListener) {
        this.mOnLoadCompleteListener = onLoadCompleteListener;
    }


    @SuppressLint("WrongConstant")
    @Override
    public void onError(Throwable e) {
        if (mLoadingErrorListener == null) return;
        ApiException apiException;
        if (!NetWorkUtils.IsNetWorkEnable(mContext)) {
            apiException = new ApiException(e, CodeException.NETWORD_ERROR, "请检查网络");
            mLoadingErrorListener.onError(apiException, iName);
            return;
        }
        if (e instanceof ApiException) {
            apiException = (ApiException) e;
            mLoadingErrorListener.onError(apiException, iName);
            Log.e("ProgressOnError", iName + "--->" + e.getMessage());
        } else if (e instanceof HttpTimeException) {
            HttpTimeException exception = (HttpTimeException) e;
            apiException = new ApiException(exception, exception.getCode(), exception.getMessage());
            mLoadingErrorListener.onError(apiException, iName);
            Log.e("ProgressOnError", iName + "--->" + e.getMessage());
        } else {
            apiException = new ApiException(e, HttpTimeException.UNKOWN_ERROR, e.getMessage());
            mLoadingErrorListener.onError(apiException, iName);
            Log.e("ProgressOnError", iName + "--->" + e.getMessage());
        }
    }

    @Override
    public void onComplete() {

    }

    @Override
    public void onSubscribe(Disposable d) {

    }

    @Override
    public void onNext(BaseResponseBean<T> entity) {
        if (entity.getCode() == 200) {
            if (mLoadingProgressListener != null) {
                mLoadingProgressListener.onLoadingResult(entity);
            }
        } else {
            if (mLoadingErrorListener != null) {
                mLoadingErrorListener.onErrorResult(entity, iName);
            }
        }
    }
}